package ch.ethz.group3.myalbum.client;

import java.util.Collection;

import ch.ethz.group3.myalbum.client.utils.Global;
import ch.ethz.group3.myalbum.client.utils.Notifier;
import ch.ethz.group3.myalbum.shared.CommentModel;
import ch.ethz.group3.myalbum.shared.InvalidFlickrTokenException;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.reveregroup.gwt.imagepreloader.FitImage;
import com.reveregroup.gwt.imagepreloader.FitImageLoadEvent;
import com.reveregroup.gwt.imagepreloader.FitImageLoadHandler;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.widgets.Img;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.layout.VStack;

public class ImageWindow extends Window {

	private PictureServiceAsync pictureService = GWT.create(PictureService.class);

	private HLayout container;
	private VStack commentpanel;
	private VLayout imagepanel;
	
	public ImageWindow() {
		setTitle("Image");
		setWidth("80%");
		setHeight("80%");
		setLayoutAlign(Alignment.LEFT);
		setShowModalMask(true);
		setIsModal(true);
		setShowMinimizeButton(false);
		setShowResizer(false);
		setShowCloseButton(true);
		setCanDragReposition(false);
		hideContextMenu();
		centerInPage();
		
		container = new HLayout();
		container.setWidth("100%");
		container.setHeight("100%");
		container.setStyleName("cacca");
//		container.setLayoutAlign(Alignment.CENTER);
//		container.setAlign(Alignment.CENTER);
		
		imagepanel = new VLayout();
		imagepanel.setWidth("53%");
		imagepanel.setHeight("100%");
		container.addMember(imagepanel);
		
		HLayout spacer = new HLayout();
		spacer.setWidth("2%");
		container.addMember(new HLayout());
		
		commentpanel = new VStack();
		commentpanel.setOverflow(Overflow.AUTO);
		commentpanel.setMembersMargin(5);  
		commentpanel.setWidth("45%");
		commentpanel.setHeight("100%");
		container.addMember(commentpanel);
		
		addItem(container);
	}
	
	public void init() {
		final FitImage aimage = new FitImage();
		aimage.setVisible(false);
		aimage.addFitImageLoadHandler(new FitImageLoadHandler() {
		    public void imageLoaded(FitImageLoadEvent event) {  
		    	 if (!event.isLoadFailed()) {
		    		 aimage.setVisible(true);
		    	 }
		    }
		});
		aimage.setUrl("https://www.google.ch/images/srpr/logo3w.png");
		aimage.setMaxSize(imagepanel.getWidth(), imagepanel.getHeight());
		imagepanel.addMember(aimage);
	}
	
	public void showImage(final ImageTileRecord image) {
		imagepanel.removeMembers(imagepanel.getMembers());
		commentpanel.removeMembers(commentpanel.getMembers());
		
		final Img bookImage = new Img();
		bookImage.setPadding(20);
		bookImage.setSrc("285.gif");
		bookImage.setWidth("128px");
		bookImage.setHeight("128px");
		bookImage.setAlign(Alignment.CENTER);
		bookImage.setLayoutAlign(VerticalAlignment.CENTER);
		commentpanel.addMember(bookImage);

		
		final FitImage aimage = new FitImage();
		aimage.setVisible(false);
		aimage.addFitImageLoadHandler(new FitImageLoadHandler() {
		    public void imageLoaded(FitImageLoadEvent event) {  
		    	 if (!event.isLoadFailed()) {
		    		 aimage.setVisible(true);
		    	 }
		    }
		});
		aimage.setUrl(image.getAttributeAsString("imageUrl"));
		aimage.setMaxSize(imagepanel.getWidth(), imagepanel.getHeight());
		imagepanel.addMember(aimage);
	
		
		pictureService.getComments(image.getAttribute("imageId"), Global.accessToken, new AsyncCallback<Collection<CommentModel>>() {
			
			@Override
			public void onSuccess(Collection<CommentModel> result) {
				commentpanel.removeChild(bookImage);
				ImageWindow.this.setTitle(image.getAttribute("name"));
				
				Label tit = new Label("Comments");
				tit.setAutoHeight();
				tit.setStyleName("title");
				commentpanel.addMember(tit);
				
				for (CommentModel comment : result) {
					VLayout cp = new VLayout();
					cp.setPadding(5);
					cp.setWidth100();
					cp.setBorder("1px solid black");
					cp.setAutoHeight();
					Label name = new Label(comment.getAuthorName());
					name.setStyleName("name");
					name.setWidth("90%");
					name.setHeight("10%");
					Label text = new Label(comment.getComment());
					text.setStyleName("text");
					text.setWidth("90%");
					text.setHeight("90%");
					
					cp.addMember(name);
					cp.addMember(text);
					commentpanel.addMember(cp);
				}
			}
			
			@Override
			public void onFailure(Throwable caught) {
				if (caught instanceof InvalidFlickrTokenException) {
					InvalidFlickrTokenException e = (InvalidFlickrTokenException) caught;
					Notifier.notify(e.getAuthorization());
				}
			}
		});
	}

}
